#include "led.h"

static void delay(volatile int ms)
{
	while(ms--);
}

int led_init(void)
{
	/* 设置 GPFCON 让 GPFCON4/5/6为 输出引脚*/
	GPFCON &= ~((3 << 8) | (3 << 10) | (3 << 12));
	GPFCON |=  ((1 << 8) | (1 << 10) | (1 << 12));

	/* 默认关闭LED */
	GPFDAT = 0x00000070;

	return 0;
}

int led1_on(void)
{

	GPFDAT &= ~(1 << 4);

	return 0;
}

int led1_off(void)
{

	GPFDAT |= (1 << 4);

	return 0;
}

int led2_on(void)
{

	GPFDAT &= ~(1 << 5);

	return 0;
}

int led2_off(void)
{

	GPFDAT |= (1 << 5);

	return 0;
}

int led3_on(void)
{

	GPFDAT &= ~(1 << 6);

	return 0;
}

int led3_off(void)
{

	GPFDAT |= (1 << 6);

	return 0;
}


int led_set_status(led_e ledx, led_status_e led_status)
{
	switch (ledx)
	{
		case LED1 : 
			if (led_status == LED_ON) 
			{
				led1_on();
			}
			else
			{
				led1_off();
			}
			break;
		case LED2 : 
			if (led_status == LED_ON) 
			{
				led2_on();
			}
			else
			{
				led2_off();
			}
			break;
		case LED3 : 
			if (led_status == LED_ON) 
			{
				led3_on();
			}
			else
			{
				led3_off();
			}
			break;
	}
}

int led_test(void)
{
	led_init();

	/* 循环点亮 */
	while (1)
	{
		led1_on();
		delay(100000);
		led1_off();
		delay(100000);

		led2_on();
		delay(100000);
		led2_off();
		delay(100000);

		led3_on();
		delay(100000);
		led3_off();
		delay(100000);
	}

	return 0;
}
